from xmlrpc.client import DateTime

from django.utils import timezone


from django.db import models
from apps.teachers.models import Teacher
from apps.paths.models import Stage, Path
from apps.tags.models import Tag
from apps.users.models import User


# 课程类别
class CourseType(models.Model):
    name = models.CharField('课程类别名', max_length=50, unique=True)

    class Meta:
        db_table = 'syl_dic_coursetype'
        verbose_name = '课程类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程是否在线类型定义

class CourseOnLineType(models.Model):
    name = models.CharField('是否在线', max_length=50, unique=True)

    class Meta:
        db_table = 'syl_dic_courseonlinetype'
        verbose_name = '课程是否在线类型定义'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程面向人员类型
class CourseFaceType(models.Model):
    name = models.CharField('面向人员类型', max_length=50, unique=True)

    class Meta:
        db_table = 'syl_dic_coursefacetype'
        verbose_name = '课程面向人员'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 课程表
class Course(models.Model):
    name = models.CharField("课程名", max_length=50, unique=True, null=False)
    pic = models.ImageField(upload_to='static/teacher_avatar/%Y%msd/', blank=True, max_length=512)
    desc = models.TextField("课程信息介绍")
    collect_num = models.IntegerField("关注数量", default=0)
    learn_count = models.IntegerField("学过人数", default=0)
    comment = models.IntegerField("评论总数", default=0)
    section_count = models.IntegerField("章节数", default=0)
    videos_count = models.IntegerField("课程数", default=0)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    # create_time = models.DateTimeField("创建时间", auto_now_add=True)
    online = models.ForeignKey(CourseOnLineType, on_delete=models.CASCADE, verbose_name="课程在线类型", null=True, default=1)
    course_type = models.ForeignKey(CourseType, on_delete=models.CASCADE, verbose_name="课程类别", null=True, default=1)
    category = models.ForeignKey(CourseFaceType, on_delete=models.CASCADE, verbose_name="课程面向类型", null=True, default=1)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="教师", null=True, default=1)
    stage = models.ForeignKey(Stage, on_delete=models.CASCADE, verbose_name="阶段", null=True, default=1)
    path = models.ForeignKey(Path, on_delete=models.CASCADE, verbose_name="路径", null=True, default=1)
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, verbose_name="标签", null=True, default=1)

    class Meta:
        db_table = "syl_course"
        verbose_name = "课程"
        verbose_name_plural = verbose_name
        ordering = ['create_time']
        unique_together = ("path", "stage")

    def __str__(self):
        return self.name


# 阶段与课程关系
class CourseStage(models.Model):
    stage = models.ForeignKey(Stage, on_delete=models.CASCADE, verbose_name='阶段')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')

    class Meta:
        db_table = 'syl_rel_coursestage'
        unique_together = ('stage', 'course')
        verbose_name = '课程表与阶段关系'
        verbose_name_plural = verbose_name


#   用户收藏课程表
class CourseCollect(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')

    class Meta:
        db_table = 'syl_rel_coursecollect'
        unique_together = ('user', 'course')

        verbose_name = '用户收藏课程表'
        verbose_name_plural = verbose_name


# 课程章节信息
class CourseSection(models.Model):
    section_name = models.CharField('章节名', max_length=50, null=False)
    course = models.ForeignKey(Course, related_name='section', on_delete=models.CASCADE, verbose_name='课程')
    seq_num = models.IntegerField('序号', default=1, null=False)

    class Meta:
        db_table = 'syl_coursesection'
        verbose_name = '课程章节'
        verbose_name_plural = verbose_name
        ordering = ['seq_num']

    def __str__(self):
        return self.section_name


# 视频对象
class CourseVideo(models.Model):
    title = models.CharField('课程视频名', max_length=50, null=False)
    paly_url = models.FileField('上传视频', upload_to='videos/%Y%m%d/', blank=True, max_length=1024)
    duration = models.IntegerField('时长（分钟）', default=0)
    seq_num = models.IntegerField('序号', default=1)
    course = models.ForeignKey(Course, related_name='course_video', on_delete=models.CASCADE, verbose_name='所属课程')
    course_section = models.ForeignKey(CourseSection, related_name='section_video', on_delete=models.CASCADE,
                                       verbose_name='所属章节')

    class Meta:
        db_table = 'syl_coursevideo'
        verbose_name = '视频'
        verbose_name_plural = verbose_name
        ordering = ['seq_num']

    def __str__(self):
        return self.title

# Create your models here.
